package com.np.contentprovider_demo1;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.np.contentprovider_demo1.utils.PermissionX;

public class MainActivity extends AppCompatActivity {

    TextView tvSendList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PermissionX.checkPermission(this, new String[]{}, 1);

        tvSendList = findViewById(R.id.tv_send_list);

        getContentResolver().registerContentObserver(Uri.parse("content://sms"), true, new ContentObserver(new Handler()) {
            @SuppressLint("Range")
            @Override
            public void onChange(boolean selfChange) {
                super.onChange(selfChange);

                // content://sms/outbox 已发送的短信
                Cursor cur = getContentResolver().query(Uri.parse("content://sms/outbox"), null, null, null, null);
                while (cur.moveToNext()) {
                    StringBuilder sb = new StringBuilder();
                    sb.append("address: ").append(cur.getString(cur.getColumnIndex("address")));
                    sb.append("; subject: ").append(cur.getString(cur.getColumnIndex("subject")));
                    sb.append("; body: ").append(cur.getString(cur.getColumnIndex("body")));
                    sb.append("; date: ").append(cur.getString(cur.getColumnIndex("date")));

                    runOnUiThread(() -> {
                        tvSendList.append(sb);
                    });
                }
            }
        });
    }
}